/*!
 Temelia - Trie tree samples.

 Copyright (C) 2008 Ceata (http://ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/samples.h"
#include <temelia/trie_tree.h>

static void sample1()
{
	int i;
	static char strings[][10] =
	{ "aa", "bbb", "ccc", "acbd", "baczs" };
	trie_tree_t t = trie_new("Root");

	PRINT("\n\n Sample 1 started \n\n");

	/*
	 * Insert in the trie tree equal keys and values, because
	 * it's easier to debug.
	 */
	for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i++)
		trie_insert(t, strings[i], strings[i]);

	//trie_remove
	PRINT(
			"Trie in preorder : (string-urile sunt ordonate lexicografic crescator)\n");
	trie_preorder(t, string_handler, NULL);

	PRINT(
			"Trie in postorder : (string-urile sunt ordonate lexicografic descrescator)\n");
	trie_postorder(t, string_handler, NULL);

	PRINT("Trie in levelorder : (string-urile sunt ordonate dupa lungime)\n");
	trie_level_order(t, string_handler, NULL);

	PRINT("Search %s %p\n", (char *) trie_search(t, "aa"),
			trie_search(t, "zar"));

	for (i = 0; i < sizeof(strings) / sizeof(strings[0]) - 2; i++)
		trie_remove(t, strings[i]);

	PRINT("Trie in preorder :\n");
	trie_preorder(t, string_handler, NULL);

	trie_delete(t);

	PRINT("\n\n Sample 1 ended \n\n");
}

static void sample2()
{
	int i;
	trie_tree_t t, u, v, w;
	static char strings[][10] =
	{ "aaa", "aab", "aac", "aad", "aae" };

	PRINT("\n\n Sample 2 started \n\n");

	t = trie_new("Root");

	for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i++)
		trie_insert(t, strings[i], strings[i]);

	PRINT("Root key : %s\n", (char *) trie_get_key(t));
	PRINT("Root's parent : %p\n", trie_get_parent(t));
	PRINT("Root's children number : %d\n", trie_get_children_number(t));
	u = trie_get_child(t, 0);
	u = trie_get_child(u, 0);
	v = trie_get_child(u, 1);
	u = trie_get_child(u, 0);

	PRINT("Keys : %s, %s\n", (char *) trie_get_key(u), (char *) trie_get_key(v));
	w = trie_new(NULL);
	PRINT("Empty : %d, %d, %d\n", trie_is_empty(v), trie_is_empty(NULL),
			trie_is_empty(w));
	PRINT("Leaf : %d, %d, %d\n", trie_is_leaf(t), trie_is_leaf(u), trie_is_leaf(NULL));
	PRINT("Root's children : %p\n", trie_get_children(t));

	trie_delete(w);
	trie_delete(t);
	PRINT("\n\n Sample 2 ended \n\n");
}

void run_trie_tree_samples()
{
	sample1();
	sample2();
}
